package cn.itsource.blog.service.impl;

import cn.itsource.blog.domain.Menu;
import cn.itsource.blog.mapper.MenuMapper;
import cn.itsource.blog.service.IMenuService;
import cn.itsource.blog.service.IMenuService;
import cn.itsource.query.BaseQuery;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class MenuServiceImpl implements IMenuService {

    @Autowired
    private MenuMapper menuMapper;

    @Override
    public List<Menu> treeMenus() {
        List<Menu> all = menuMapper.getAll();

        List<Menu> menus = new ArrayList<>();
        //把List集合转为Map集合
        Map<Long, Menu> map = all.stream().collect(Collectors.toMap(Menu::getId, menu -> menu));

        for (Menu menu : all) {
            if(menu.getParentId()==null){
                menus.add(menu);
            }else{
                Menu type = map.get(menu.getParentId());
                if(type!=null){
                    type.getChildren().add(menu);
                }

            }
        }

        return menus;
    }


}
